﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP09S03_OfficeTracking
{
    public partial class FrmEquipment : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        public FrmEquipment()
        {
            InitializeComponent();
            ctrlItemSearch1.btnSearch.Click += new EventHandler(btnSearch_Click);
        }
        int itemAdded;
        string itemID;
        private void FrmEquipment_Load(object sender, EventArgs e)
        {
      
            try
            {
               
                //this.statusTableAdapter.Fill(this.dSItem.Status);
                this.deviceStatusTableAdapter.Fill(this.dSItem.DeviceStatus); 
               this.categoryTableAdapter.Fill(this.dSItem.Category);
                this.officeItemsTableAdapter.FillByConsumable(this.dSItem.OfficeItems, false);
                itemAdded = 0;
                itemID = "";
            }
            catch (Exception ex) {

                CatchError(ex);
            }

        }



        private void btnSearch_Click(object sender, EventArgs e)
        {

            try
            {
                this.dSItem.OfficeItems.Clear();
                this.dSItem.ItemDetails.Clear();

                this.officeItemsTableAdapter.FillBySearch(this.dSItem.OfficeItems, ctrlItemSearch1.TEXTBOX_ID.Text, ctrlItemSearch1.TEXTBOX_Name.Text, (int)ctrlItemSearch1.ComboBox_Category.SelectedValue, ctrlItemSearch1.TEXTBOX_Brand.Text, ctrlItemSearch1.TEXTBOX_Model.Text, false );

                updateTotal();
            }
            catch (Exception ex)
            {

                CatchError(ex);
            }



        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = gvItem.CurrentRow;

            if (row == null)
            {
                MessageBox.Show("No Record Seletectd");
                return;
            }

            int con = 0;

            if (row.IsNewRow == false)
            {
                //check items belong to category.

                Controller.ItemController ic = new Controller.ItemController();

                con = ic.CheckItemID(row.Cells[0].Value.ToString());

            }

            if (con > 0)
            {

                CatchError(new Exception("Cannot delete this one because this one already has some associate transaction !"));

            }
            else
            {
                if (ShowDeleteConfirm() == DialogResult.Yes)
                {
                    gvItem.Rows.Remove(row);
                    updateTotal();
                    this.dSItem.ItemDetails.Clear();
                }
            }


         
        }

        private void gvItem_SelectionChanged(object sender, EventArgs e)
        {
   
            DataGridViewRow row = gvItem.CurrentRow;

            if (row == null)
            {
                return;
            }

            this.dSItem.ItemDetails.Clear();

           itemID = row.Cells[0].Value.ToString();

            this.itemDetailsTableAdapter.FillByItemID(this.dSItem.ItemDetails, itemID);
            this.dSItem.ItemDetails.ItemIDColumn.DefaultValue = itemID;
            this.dSItem.ItemDetails.ItemDetailIDColumn.DefaultValue = itemID ;
            this.dSItem.ItemDetails.CreateDateColumn.DefaultValue = DateTime.Now;

            SetMessage("Total " + this.dSItem.ItemDetails.Count.ToString() + " Records!");

        }

        private void gvItem_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            updateTotal();
            if (e.RowIndex > -1)
            {
                btnDelete.Enabled = true;

            }
            else
            {
                btnDelete.Enabled = false;
            }
        }

        private void dgvItemDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                btnDelete2.Enabled = true;

            }
            else
            {
                btnDelete2.Enabled = false;
            }
        }


        private void updateTotal()
        {
            SetMessage("Total " + this.dSItem.OfficeItems.Count.ToString() + " Sub Records!");

        }

        private void gvItem_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

            e.Cancel = true;
            CatchError(new Exception("Invalid Data entered!"));
        }

        private void dgvItemDetails_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

            e.Cancel = true;
            CatchError(new Exception("Invalid Data entered!"));

            MessageBox.Show(e.Exception.Message);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                gvItem.EndEdit();
                this.officeItemsTableAdapter.Update(this.dSItem.OfficeItems);
                SetMessage("Update Successfully!");
            }
            catch (Exception ex)
            {
                CatchError(ex);
            }
        }

        private void btnSave2_Click(object sender, EventArgs e)
        {
            try
            {
                dgvItemDetails.EndEdit();
                this.itemDetailsTableAdapter.Update(this.dSItem.ItemDetails );
                SetMessage("Update Successfully!");
            }
            catch (Exception ex)
            {
                CatchError(ex);
            }
        }

        private void btnDelete2_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dgvItemDetails.CurrentRow;

            if (row == null)
            {
                MessageBox.Show("No Record Seletectd");
                return;
            }

            int con = 0;

            if (row.IsNewRow == false)
            {
                //check items belong to category.

                Controller.ItemController ic = new Controller.ItemController();

                con = ic.CheckItemDetailID(row.Cells[0].Value.ToString());

            }

            if (con > 0)
            {

                CatchError(new Exception("Cannot delete this one because this one already has some associate transaction !"));

            }
            else
            {
                if (ShowDeleteConfirm() == DialogResult.Yes)
                {
                    dgvItemDetails.Rows.Remove(row);
                    updateTotal();
                }
            }
        }

        public void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                gvItem.EndEdit();
                this.officeItemsTableAdapter.Update(this.dSItem.OfficeItems);
            }
            catch (Exception ex)
            {
                CatchError(ex);
            }

            try
            {
                DiagAddEquipment d = new DiagAddEquipment();
                d.ShowDialog();
                this.dSItem.OfficeItems.Clear();
                this.officeItemsTableAdapter.FillByConsumable(this.dSItem.OfficeItems,false );
                updateTotal();

            }
            catch (Exception ex)
            {
                CatchError(ex);
            }
        }



        private void dgvItemDetails_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {

            if (itemAdded > 9)
            {
                SetMessage("Maximum can add 10 items once, please try it next time");

            }
            dgvItemDetails.Rows[dgvItemDetails.NewRowIndex-1].Cells[0].Value = itemID + "#" + Convert.ToSingle(itemAdded);

            //MessageBox.Show(dgvItemDetails.NewRowIndex.ToString());

             itemAdded =itemAdded+1;

             if (itemAdded == 10) {

                 dgvItemDetails.AllowUserToAddRows = false;
                 SetMessage("Maximum can add 10 items once.");
             }

        }

        private void panelContext_Paint(object sender, PaintEventArgs e)
        {

        }

        
    }
}
